#include "stdafx.h"
#include "GUIController.h"
#include "InputManager.h"

string layout = "none";
string scheme = "none";

bool GUIController::m_isInitialized = 0;

CEGUI::OgreRenderer* GUIController::m_Renderer;

GUIController::GUIController (Model *model, Ogre::RenderWindow* window, string scheme){
	if (m_isInitialized == 0) {
		m_isInitialized = false;
	}
	setModel(model);
	if (!m_isInitialized) {
		GUIController::initializeCEGUI(scheme);
		m_isInitialized = true;
	}
	InputManager::getInstance()->registerObserver(this);
}

GUIController::~GUIController () {

}
	
bool GUIController::update(float deltaTime) {
	GUIController::updateGUI(deltaTime);
	OIS::Keyboard* keyboard = InputManager::getInstance()->getKeyboard();
  if(keyboard->isKeyDown(OIS::KC_ESCAPE)) {
  	return false;
	}
	
  return true;
}

bool GUIController::keyPressed(const OIS::KeyEvent &arg) {
	CEGUI::System &sys = CEGUI::System::getSingleton();
	sys.injectKeyDown(arg.key);
	sys.injectChar(arg.text);
	return true;
}

bool GUIController::keyReleased(const OIS::KeyEvent &arg) {
	CEGUI::System::getSingleton().injectKeyUp(arg.key);
	return true;
}

bool GUIController::mouseMoved( const OIS::MouseEvent &arg ) {
	CEGUI::System &sys = CEGUI::System::getSingleton();
	sys.injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
	// Scroll wheel. Not currently used
	if (arg.state.Z.rel) {
		sys.injectMouseWheelChange(arg.state.Z.rel / 120.0f);
	}
	return true;
}

bool GUIController::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) {
	CEGUI::System::getSingleton().injectMouseButtonDown(GUIController::convertButton(id));
	return true;
}

bool GUIController::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) {
	CEGUI::System::getSingleton().injectMouseButtonUp(GUIController::convertButton(id));
	return true;
}

void GUIController::initializeCEGUI(string scheme) {
	// Initialize CEGUI
	GUIController::m_Renderer = &CEGUI::OgreRenderer::bootstrapSystem();
	
	// Now that CEGUI has been initialized, we need to set the so-called default resource groups for each of CEGUI'S resource managers. 
	CEGUI::Imageset::setDefaultResourceGroup("Imagesets");
	CEGUI::Font::setDefaultResourceGroup("Fonts");
	CEGUI::Scheme::setDefaultResourceGroup("Schemes");
	CEGUI::WidgetLookManager::setDefaultResourceGroup("LookNFeel");
	CEGUI::WindowManager::setDefaultResourceGroup("Layouts");

	// The following line of code selects the skin:
	CEGUI::SchemeManager::getSingleton().create(scheme + ".scheme");
}

void GUIController::updateGUI(float deltaTime) {
	CEGUI::System::getSingleton().injectTimePulse(deltaTime);
}

void GUIController::setGUIsheet(string layoutName) {
	// Add exception handling here if a layout isn't used!
	if (layoutName != "none") {
		CEGUI::Window *guiRoot = CEGUI::WindowManager::getSingleton().loadWindowLayout(layoutName +".layout"); 
		CEGUI::System::getSingleton().setGUISheet(guiRoot);
	}	
}


CEGUI::MouseButton GUIController::convertButton(OIS::MouseButtonID buttonID) {
	switch (buttonID) {
    case OIS::MB_Left:
        return CEGUI::LeftButton;
 
    case OIS::MB_Right:
        return CEGUI::RightButton;
 
    case OIS::MB_Middle:
        return CEGUI::MiddleButton;
 
    default:
        return CEGUI::LeftButton;
    }
}